<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Project 1B By DohunKim</title>
</head>

<h1> Project 1B </h1>
<h2> By Dohun Kim (4-15-2012) </h2><br />

<p>
<form action = "query.php" name = "SQLquery" method = "post">
<textarea name="query" cols="60" rows="8"></textarea>
<input type="submit" value="Submit" />
</form>
</p>

<?php /* following code written with help from http://www.programming-web.com/index.php?option=com_content&view=article&id=11:retrieving-data-from-mysql-into-html-table-using-php&catid=4:php-basics&Itemid=10*/

$connect = mysql_connect("localhost", "cs143")
or die("Error, Could not Connect.");

mysql_select_db("CS143");
$expr = $_POST["query"];

if($expr == '')
{ 
	unset($expr);
}

if(isset($expr)) {

$request = mysql_query($expr); 

$num_col = @mysql_num_fields($request);

echo " <table border =\"1\" style=\"background-color:#CED8F6;\">
<caption><EM>Result of SQL Search</EM></caption><tr>";

for($ii=0;$ii<$num_col;$ii++)
{
	echo "<th>" . mysql_field_name($request, $ii) . "</th>";
}

// Following Code written with help from
// http://www.php.net/manual/en/function.mysql-fetch-array.php
// Mainly the part on mysql_fetch_array
while($row = @mysql_fetch_array($request))
{
	echo "<tr>";
	for($jj=0; $jj<$num_col; $jj++)
	{
		if($row[$jj] == NULL) // If there is no value, such as for the Date of Death, then output NA
			echo "<td>" . "NULL" . "</td>"; 
		else
			echo "<td>" . $row[$jj] . "</td>";
		
	}
	echo "</tr>";
}
echo "</table>"; // End Table 

} // end if(isset($expr));

mysql_close($connect); // Close connection to MySQL databases
?>
</body>
</html>